<template>
  <div class="login-container">
    <div class="login-box">
      <div class="inner-box">
        <div class="logo">
          <img src="../../assets/images/login_bg.jpg" alt="">
          <div class="sys-title"> 麻城市宏远路桥信息化管理平台</div>
        </div>
        <div class="line" />

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

          <div class="title-container">
            <h3 class="title">用户登录</h3>
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model.trim="loginForm.username"
              placeholder="用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model.trim="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;font-size: 16px;line-height: 24px;" @click.native.prevent="handleLogin">登 录</el-button>

          <!-- <div class="tips">
            <span style="margin-right:20px;">username: admin</span>
            <span> password: any</span>
          </div> -->

        </el-form>
      </div>

    </div>
    <div class="login-bottom">
      <a style="color:#666" target="_blank" href="https://beian.miit.gov.cn/">网站备案号：鄂ICP备 2021020136号-1</a>
      <img class="img" style="border: 0;" src="../../assets/images/bah.png" alt="">
      <a style="color:#666;margin-left: 20px;" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=42118102000302">鄂公网安备：42118102000302</a>
      <!-- <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=42118102000302">鄂公网安备：42118102000302</a> -->
    </div>
  </div>
</template>

<script>
import { required } from '@/utils/validate'
const md5 = require('md5')
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!required(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePwd = (rule, value, callback) => {
      if (!required(value)) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePwd }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const params = {
            account: this.loginForm.username,
            password: md5(this.loginForm.password)
          }
          this.$store.dispatch('user/login', params).then(() => {
            // this.$router.push({ path: this.redirect || '/' })
            // 清除贴标签
            this.$store.state.tagsView.visitedViews = []
            // 跳转主页
            this.$router.push({ path: '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#E5E5E5;
$light_gray:#000000;
$cursor: #000000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    margin-bottom: 28px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: relative;
  .login-box {
    height: 510px;
    width: 1000px;
    background: #fff;
    padding: 94px 66px 70px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .inner-box{
      height: 100%;
      width: 100%;
      display: flex;
      align-items: center;
      border-bottom: 2px solid #5589c5;
      .logo{
        height: 100%;
        width: 500px;
        position: relative;
        >img{
          object-fit: cover;
          height: 100%;
          width: 100%;
        }
        .sys-title{
          position: absolute;
          color: #060eef;
          font-size: 24px;
          line-height: 80px;
          width: 100%;
          text-align: center;
          top: 0;
          left: 0;
          font-weight: bold;
          letter-spacing: 2px;
        }
      }
      .line{
        height: 234px;
        border-right: 1px solid #5f88c0;
        margin: 0 44px 0 20px;
      }
    }
  }
  .login-bottom {
    position: absolute;
    bottom: 15px;
    width: 100%;
    text-align: center;
    /* left: 40%; */
    .img {
      position: relative;
      left: 20px;
      vertical-align: baseline;
      top: 5px;
      margin-right: 5px;
    }
  }
  min-height: 100%;
  width: 100%;
  background-color: #eff0f4;
  overflow: hidden;

  .login-form {
    position: relative;
    // width: 520px;
    // max-width: 100%;
    // padding: 160px 35px 0;
    // margin: 0 auto;
    overflow: hidden;
    flex: 1;
    align-self: flex-start;
    margin-top: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      // color: $light_gray;
      color: #5c8fcd;
      // margin: 0px auto 40px auto;
      margin: 0 0 10px;
      // text-align: center;
      font-weight: bold;
      font-size: 20px;
      line-height: 50px;
      border-bottom: 1px solid #5c8fcd;
      position: relative;
      &::before{
        position: absolute;
        bottom: -1px;
        left: 0;
        content: '';
        height: 2px;
        width: 100px;
        background: #5c8fcd;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
